package com.ojoin.trade.common.utils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import com.ojoin.trade.common.utils.json.JSONUtils;

public class URLUtils {
	public static String getJsonByUrl(String urL) throws Exception {

		HttpClient httpClient = new SSLClient();
		
		HttpGet get = new HttpGet(urL);
		
		HttpResponse response = httpClient.execute(get);

		
		if (response.getStatusLine().getStatusCode() != 200) {
			throw new IOException(response.getStatusLine().toString());
		}
		
		return EntityUtils.toString(response.getEntity(), "gbk");
	}
	
	public static String getJsonByUrl(String cookies,String urL) throws Exception {

		RequestConfig defaultRequestConfig = RequestConfig
				.custom().setSocketTimeout(120 * 1000)
				.setConnectTimeout(120 * 1000).build();

		CloseableHttpClient httpclient = HttpClients
				.custom()
				.setUserAgent(
						"Mozilla/5.0 Firefox/26.0")
				.setMaxConnTotal(120)
				.setMaxConnPerRoute(120)
				.setDefaultRequestConfig(
						defaultRequestConfig).build();

		HttpGet get = new HttpGet(urL);
 
		
		get.addHeader(new BasicHeader("Cookie",cookies));
		HttpResponse response = httpclient.execute(get);

		if (response.getStatusLine().getStatusCode() != 200) {
			throw new IOException(response.getStatusLine().toString());
		}
		return EntityUtils.toString(response.getEntity(), "gbk");
	}
	
	public static HttpResponse isLoginByUrl(String cookies,String urL) throws Exception {

		RequestConfig defaultRequestConfig = RequestConfig
				.custom().setSocketTimeout(120 * 1000)
				.setConnectTimeout(120 * 1000).build();

		CloseableHttpClient httpclient = HttpClients
				.custom()
				.setUserAgent(
						"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36")
				//.setMaxConnTotal(120)
				//.setMaxConnPerRoute(120)
				.setDefaultRequestConfig(
						defaultRequestConfig).build();

		HttpGet get = new HttpGet(urL);
 
		
		get.addHeader(new BasicHeader("Cookie",cookies));

		return httpclient.execute(get);
	}
	

	public static <T> T getJsonByUrl(String urL, Class<T> clazz)
			throws Exception {
		String str = getJsonByUrl(urL);
		
		if(!CommonUtils.isEmpty(str) && !str.equals("\"\"")){
			return JSONUtils.parseObject(str, clazz);
		}else {
			return null;
		}
		

	}

	public   static String mapToQParams(Map<String,String> queryString){
		StringBuilder sb = new StringBuilder();
		for(Map.Entry<String, String> e : queryString.entrySet()){
			if(sb.length() > 0){
				sb.append('&');
			}
			try {
				sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append('=').append(URLEncoder.encode(e.getValue(), "UTF-8"));
			}   catch (Exception ex){
				throw CommonUtils.unchecked(ex);
			}
		}
		return sb.toString();
	}

}
